package base.utils;

import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;

/**
 *@TODO ：
 * @auther:	louris		
 * @email :	lzhang@gypsii.com
 * @date  ：2012-2-28  上午10:57:32
 */
public class DESUtil {
	
	//加解密的32为key
	private static final byte[] key = Base64.decode("ZmRkZDVjZjEwNTI5MTY0ZWFlN2IwZWVm".getBytes(),Base64.NO_WRAP);

	/**
     * ECB加密,不要IV
     * @param key 密钥
     * @param data 明文
     * @return Base64编码的密文
     * @throws Exception
     */
    public static String des3EncodeECB(String data) throws Exception {

        DESedeKeySpec spec = new DESedeKeySpec(key);
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
        Key deskey = keyfactory.generateSecret(spec);

        Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding");

        cipher.init(Cipher.ENCRYPT_MODE, deskey);
        byte[] bOut = cipher.doFinal(data.getBytes("UTF-8"));
        return new String(Base64.encode(bOut,  Base64.NO_WRAP),"UTF-8");
    }

    /**
     * ECB解密,不要IV
     * @param key 密钥
     * @param data Base64编码的密文
     * @return 明文
     * @throws Exception
     */
    public static byte[] ees3DecodeECB(byte[] data) throws Exception {

        DESedeKeySpec spec = new DESedeKeySpec(key);
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
        Key deskey = keyfactory.generateSecret(spec);

        Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding");

        cipher.init(Cipher.DECRYPT_MODE, deskey);

        byte[] bOut = cipher.doFinal(Base64.encode(data,  Base64.NO_WRAP));

        return bOut;

    }

    /**
     * CBC加密
     * @param key 密钥
     * @param keyiv IV
     * @param data 明文
     * @return Base64编码的密文
     * @throws Exception
     */
    public static byte[] des3EncodeCBC(byte[] keyiv, byte[] data)
            throws Exception {

        Key deskey = null;
        DESedeKeySpec spec = new DESedeKeySpec(key);
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
        deskey = keyfactory.generateSecret(spec);

        Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
        IvParameterSpec ips = new IvParameterSpec(keyiv);
        cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
        byte[] bOut = cipher.doFinal(data);

        return bOut;
    }

    /**
     * CBC解密
     * @param key 密钥
     * @param keyiv IV
     * @param data Base64编码的密文
     * @return 明文
     * @throws Exception
     */
    public static byte[] des3DecodeCBC(byte[] keyiv, byte[] data)
            throws Exception {

        Key deskey = null;
        DESedeKeySpec spec = new DESedeKeySpec(key);
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
        deskey = keyfactory.generateSecret(spec);

        Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
        IvParameterSpec ips = new IvParameterSpec(keyiv);

        cipher.init(Cipher.DECRYPT_MODE, deskey, ips);

        byte[] bOut = cipher.doFinal(data);

        return bOut;

    }
    
    public static void testKey(String pass){
    	
//    	byte[] keyiv = { 1, 2, 3, 4, 5, 6, 7, 8 };

		try {
			
			 System.out.println("ECB加密解密");
		        String str3 = des3EncodeECB(pass);
		        System.out.println(str3);
		        byte[] str4 = ees3DecodeECB(str3.getBytes());
		        System.out.println(new String(str4, "UTF-8"));

		        System.out.println();

//		        System.out.println("CBC加密解密");
//		        byte[] str5 = des3EncodeCBC(keyiv, data);
//		        byte[] str6 = des3DecodeCBC(keyiv, str5);
//		        System.out.println(new String(Base64.encode(str5,  Base64.DEFAULT),"UTF-8"));
//		        System.out.println(new String(str6, "UTF-8"));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
       


    }
}
